//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web.UI.WebControls;
using CommunityServer.Blogs.Components;
using CommunityServer.Components;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;

namespace CommunityServer.ControlPanel.Blogs
{
	/// <summary>
	/// Summary description for PostEditorPage.
	/// </summary>
	public class PostCategoriesListPage : BaseBlogPage
	{
		protected CommunityServer.ControlPanel.Controls.ControlPanelSelectedNavigation SelectedNavigation1;
		protected CommunityServer.Controls.MPContent DescriptionRegion;
		protected CommunityServer.Controls.MPContent TaskRegion;
		protected CommunityServer.Controls.MPContainer MPContainer;
		protected CommunityServer.ControlPanel.Controls.PanelTabControl PanelTabControl1;
        protected Repeater theTags;
        protected ModalLink NewCategory;
        
        //NewCategory

	
		private void Page_Load(object sender, System.EventArgs e)
		{
            NewCategory.Url = string.Format("CategoryForm.aspx?Sectionid={0}&categoryid={1}",CurrentWeblog.SectionID,"-1");
            JavaScript.RegisterRefresh(this);
            theTags.ItemCreated +=new RepeaterItemEventHandler(theTags_ItemCreated);
            theTags.ItemCommand +=new RepeaterCommandEventHandler(theTags_ItemCommand);
			if(!IsPostBack)
			{
			    ArrayList al = PostCategories.GetCategories(CurrentWeblog.SectionID,false,true, -1, true);
           
                theTags.DataSource = al;
                theTags.DataBind();
			}
		}

        protected string LastPostDesc(object obj)
        {
            if(obj == null || obj.ToString() == "Jan 01 1753, 12:00 AM")
                return string.Empty;
            else
                return obj.ToString();
        }

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);

		}
        #endregion

        private void theTags_ItemCreated(object sender, RepeaterItemEventArgs e)
        {
            if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                LinkButton lb = e.Item.FindControl("DeleteButton") as LinkButton;
                if(lb != null)
                {
                    lb.Attributes.Add("onclick", "return confirm('" + CommunityServer.ControlPanel.Components.ResourceManager.GetString("CP_Blogs_Categories_Delete_Warning") + "');");
                }

                LiteralOrLink lorl = e.Item.FindControl("PostsLink") as LiteralOrLink;
                if(lorl != null)
                {
                    PostCategory pc = e.Item.DataItem as PostCategory;
                    lorl.TextOnly = pc.TotalThreads <= 0;
                }
            }
        }

        private void theTags_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if(e.CommandName == "Delete")
            {
                int categoryid = Globals.SafeInt(e.CommandArgument.ToString(), -1);
                if(categoryid > -1)
                {
                    PostCategory pc = PostCategories.GetCategory(categoryid,this.CurrentWeblog.SectionID,true);
                    if(pc != null)
                    {
                       PostCategories.DeleteCategory(pc.CategoryID,this.CurrentWeblog.SectionID);
                    }
                }

            }

            Response.Redirect(Request.Url.ToString());
            Response.End();
        }
    }
}
